﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BisinessLogic;
using Domain.Entities;
using Web.ViewModels;

namespace Web.Controllers
{
    public class StoreController : Controller
    {
        private DataManager dataManager;

        public StoreController(DataManager dataManager)
        {
            this.dataManager = dataManager;
        }
 
        int GetStoreIdByProduceAndPackage(int packId, int produceId)
        {
            return dataManager.ProduceInPackages.GetProduceInPackages()
                .FirstOrDefault(i => i.PrepackageId == packId && i.ProduceId == produceId).Id;
        }

        [HttpPost]    
        public ActionResult GetStoreByProduceWithPackage(int oldStId,int pId)
        {    // получим переменную слада со старым пакетом
            var oldStore = dataManager.ProduceInPackages.GetProduceInPackageById(oldStId);
            // получим новый ид на складе
            int newStoreId = GetStoreIdByProduceAndPackage(pId, oldStore.ProduceId);
            // новый обьект хранилища.
            var store = dataManager.ProduceInPackages.GetProduceInPackageById(newStoreId);

            return Json(new { price = store.Price ,
                              storeId = store.Id,
                              success = true
            });

        }
        [HttpGet]
        public ActionResult Browse()
        {

            if (Request["Category"] == null)
            {
                return RedirectToAction("Catalog", "Home");
            }

            var category=dataManager.Categories.GetCategoryByEnglishName(Request["Category"]);
           
            var pr =
                dataManager.ProduceInPackages.GetProduceInPackages().Where(c=>c.Count>0)
                    .Select(i => i.Produce)
                    .Distinct()
                    .Where(i => i.Category.EnglishName == Request["Category"]).ToList();
    
            var list = new List<ItemsForSale>();

            foreach (var p in pr)
            {
                var packages = dataManager.ProduceInPackages.GetPrepackagesByProduce(p).ToList();
                int storeId = GetStoreIdByProduceAndPackage(packages.FirstOrDefault().Id, p.Id);
                list.Add(new ItemsForSale
                {
                    ProduceId = p.Id,
                    Name = p.Name,
                    Category = p.Category.Name,
                    About = p.About,
                    ProductArtUrl = "~\\UploadedFiles\\" + p.ProductArtUrl,
                    Producer = p.Producer.Country,
                    Packages = packages,
                    Id = storeId,
                    Price = dataManager.ProduceInPackages.GetProduceInPackageById(storeId).Price
                }
                );
            }

            var cat = new CatalogProduces
            {
                Name = category.Name,
                Items = list
            };
       
            return View(cat);
        }
        public ActionResult GenreMenu()
        {
            return PartialView(dataManager.Categories.GetCategories().ToList());
        }

        [HttpGet]
        public ActionResult Search()
        {
            var cat = new CatalogProduces
            {
                Name = "Результат поиска"
            };
            var plist = new List<Produce>();
            var list = new List<ItemsForSale>();
            string searchstring = Request["searchstring"];
            if (String.IsNullOrEmpty(searchstring)) return View(cat);

            plist =
                dataManager.ProduceInPackages.GetProduceInPackages().Where(c => c.Count > 0)
                    .Select(i => i.Produce)
                    .Distinct()
                    .Where(i => i.Name.ToLower().Contains(searchstring.ToLower())).ToList();

            if (plist.Any())
            {
                list.AddRange(from p in plist
                    let packages = dataManager.ProduceInPackages.GetPrepackagesByProduce(p).ToList()
                    let storeId = GetStoreIdByProduceAndPackage(packages.FirstOrDefault().Id, p.Id)
                    select new ItemsForSale
                    {
                        ProduceId = p.Id, Name = p.Name, Category = p.Category.Name, About = p.About, ProductArtUrl = "~\\UploadedFiles\\" + p.ProductArtUrl, Producer = p.Producer.Country, Packages = packages, Id = storeId, Price = dataManager.ProduceInPackages.GetProduceInPackageById(storeId).Price
                    });
            }
            cat.Items = list;
            return View(cat);

        }


    }
}
